<?php
namespace Admin\Controller;
use Think\Controller;
class MemberController extends CommonController {	
 public function index(){
		$model = D('Member');
		$info = $model->indexDo();
		$this -> assign('member',$info['member']);
		$this -> assign('page',$info['page']);
    	$this -> display();
	}
	//用户添加
	public function add(){
		$role = M('role') -> select();
		$this -> assign('role',$role);
		$this -> display();
	}
	//用户添加处理
	public function addDO(){
		if(!IS_POST){
			$this -> error('非法提交数据');
		}else{
			$model = D('Member');
			if(!$model -> create()){
				$this -> error($model -> getError());
			}else{
				$info = $model -> addDo();
				if($info == 0){
					$this -> error('两次密码不相同');
					exit;
				}
				if($info == 1){
					$this -> success('添加成功',U('Member/index'));
				}	
			}
		}	
	}
	//用户编辑
	public function edit(){
		$id =I('id','','intval');
		$result = D('member');
		//SELECT b.id,name,remark FROM tp_role_user AS a, tp_role AS b WHERE a.role_id = b.id AND a. user_id='2'
		$MemberRole = $result -> query( "SELECT b.id,name,remark FROM yx_role_user AS a, yx_role AS b WHERE a.role_id = b.id AND a. user_id=".$id );		
		$this -> assign('MemberRole',$MemberRole);

		$member = M('member')->where(array('id'=>$id))->find();
		$this -> assign('member',$member);
		$role = M('role') -> select();
		$this -> assign('role',$role);
		$this -> display();	
	}
	//用户编辑处理
	public function editDo(){
		if(!IS_POST){
			$this -> error('非法提交数据');
		}else{
			$model = D('Member');
			$info = $model -> editDo();
			switch($info){
				case 0:
					$this -> error('两次密码不相同');
					break;
				case 1:
					$this ->success('修改成功',U('Member/index'));
					break;	
			}
		}
	}
	//用户单个删除
	public function del(){
		$id = I('id','','intval');
		M('role_user') -> where(array('user_id' => $id)) -> delete();	
		$info = M('member')->where('id='.$id)->delete(); // 删除id为5的用户数据
		if(false !== $info){
			$this -> redirect('Member/index');
		}else{
			$this -> error('删除失败');
		}
	}
	//Ajax批量删除用户
	public function ajaxDel(){
		$ids = I('id');
		$ids = explode(',',$ids);
		M('role_user') -> where(array('user_id' => array('IN',$ids))) -> delete();	
		$info = M('member')->where(array('id '=> array('IN',$ids)))->delete();
		echo 1;
	}	

}